package com.system.uitls;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.UUID;

public class CommonUtil
{
  public static String nullToEmptyString(Object str)
  {
    try
    {
      if ((str == null) || ("null".equals(str)) || ("".equals(String.valueOf(str)))) {
        return "";
      }

      return String.valueOf(str).trim(); } catch (Exception e) {
    }
    return "";
  }

  public static int convertToInt(String src, int defaultval)
  {
    try
    {
      return Integer.parseInt(src); } catch (Exception e) {
    }
    return defaultval;
  }

  public static long convertToLong(String src, long defaultVal)
  {
    try
    {
      return Long.parseLong(src); } catch (Exception e) {
    }
    return defaultVal;
  }

  public static String valueOf(Object obj)
  {
    return (obj == null) || ("null".equals(obj)) ? "" : obj.toString();
  }

  public static boolean notNulls(String s)
  {
    return (s != null) && (!"".equals(s.trim())) && (!"null".equals(s));
  }

  public static String getFormatCurDate(String format)
  {
    SimpleDateFormat sdf = new SimpleDateFormat(format);
    Calendar calendar = Calendar.getInstance();
    try {
      return sdf.format(new Date(calendar.getTimeInMillis())); } catch (Exception e) {
    }
    return "";
  }

  public static String getCurDate()
  {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Calendar calendar = Calendar.getInstance();
    try {
      return sdf.format(new Date(calendar.getTimeInMillis())); } catch (Exception e) {
    }
    return "";
  }

  public static String getFormatCurDate(Date d, String format)
  {
    SimpleDateFormat sdf = new SimpleDateFormat(format);
    try {
      return sdf.format(d); } catch (Exception e) {
    }
    return "";
  }

  public static Date getDate(String d, String format)
  {
    if (format == null)
      format = "yyyy-MM-dd";
    SimpleDateFormat sdf = new SimpleDateFormat(format);
    try {
      return sdf.parse(d); } catch (ParseException e) {
    }
    return null;
  }

  public static String getUuid()
  {
    return UUID.randomUUID().toString().replace("-", "");
  }
}